<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateCartAPIRequest;
use App\Http\Requests\API\UpdateCartAPIRequest;
use App\Models\Cart;
use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Repositories\CartRepository;
use App\Repositories\GoodsRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class CartController
 * @package App\Http\Controllers\API
 */

class CartAPIController extends ApiBaseController
{
    /** @var  CartRepository */
    private $cartRepository;
    private $goodsRepository;
    protected $model_name = 'App\Models\Cart';
    protected $model_load_array = [
    ];

    public function __construct(CartRepository $cartRepo, GoodsRepository $goodsRepository)
    {
        $this->cartRepository = $cartRepo;
        $this->goodsRepository = $goodsRepository;
    }

    public function authLimit()
    {
        return [
            ['user_id', '=', Auth::id()],
            ['number', '>', 0],
        ];
    }

    public function add(Request $request)
    {
        $goods_id = $request->goods_id;
        $attribute_id = $request->attribute_id;
        $number = request('number',1);
        $goods = Goods::find($goods_id);
        if($attribute_id){
            $attribute = GoodsAttribute::find($attribute_id);
        }else{
            $attribute = true;
        }
        if($goods && $attribute){
            if(($goods->status!=1)&&$number>0)
                return responseZK(0,['number'=>0],'该产品已下架');
            if((($attribute instanceof GoodsAttribute) && $attribute->status!=1)&&$number>0)
                return responseZK(0,['number'=>0],'该产品已下架');

            $ok = $this->goodsRepository->checkNumber($number,$goods,$attribute);
            if(!$ok){
                if($attribute && ($attribute instanceof GoodsAttribute)){
                    $_number = $attribute->number;
                }else{
                    $_number = $goods->number;
                }
                return responseZK(0,['number'=>$_number],'该产品已超出可购买数量');
            }
            $ok = $this->goodsRepository->checkDayNumber($number,$goods,$attribute);
            if(!$ok){
                if($attribute && ($attribute instanceof GoodsAttribute)){
                    $_number = $attribute->day_buy_number;
                }else{
                    $_number = $goods->day_buy_number;
                }
                return responseZK(0,['number'=>$_number],'超过产品单日购买上限');
            }

            $cart = Cart::where([
                'user_id'=>Auth::id(),
                'goods_id'=>$goods_id,
                'attribute_id'=>$attribute_id
            ])->first();
            if($cart){
                $cart->number += $number;
                if($cart->number>0){
                    $cart->save();
                }else{
                    $cart->delete();
                }
            }else{
                if($number>0){
                    Cart::create([
                        'user_id'=>Auth::id(),
                        'number'=>$number,
                        'goods_id'=>$goods_id,
                        'attribute_id'=>$attribute_id
                    ]);
                }
            }
            return responseZK(1,null,'添加成功');
        }else{
            return responseZK(0,null,'没有商品或属性');
        }
    }

    public function setNumber(Request $request)
    {
        $goods_id = $request->goods_id;
        $attribute_id = $request->attribute_id;
        $number = request('number',1);
        $goods = Goods::find($goods_id);
        if($attribute_id){
            $attribute = GoodsAttribute::find($attribute_id);
        }else{
            $attribute = true;
        }
        if($goods && $attribute){
            $ok = $this->goodsRepository->checkNumber($number,$goods,$attribute);
            if(!$ok){
                if($attribute && ($attribute instanceof GoodsAttribute)){
                    $_number = $attribute->number;
                }else{
                    $_number = $goods->number;
                }
                return responseZK(0,['number'=>$_number],'该产品已超出可购买数量');
            }
            $ok = $this->goodsRepository->checkDayNumber($number,$goods,$attribute);
            if(!$ok){
                if($attribute && ($attribute instanceof GoodsAttribute)){
                    $_number = $attribute->day_buy_number;
                }else{
                    $_number = $goods->day_buy_number;
                }
                return responseZK(0,['number'=>$_number],'超过产品单日购买上限');
            }

            $cart = Cart::where([
                'user_id'=>Auth::id(),
                'goods_id'=>$goods_id,
                'attribute_id'=>$attribute_id
            ])->first();
            if($cart){
                $cart->number = $number;
                if($cart->number>0){
                    $cart->save();
                }else{
                    $cart->delete();
                }
            }else{
                if($number>0){
                    Cart::create([
                        'user_id'=>Auth::id(),
                        'number'=>$number,
                        'goods_id'=>$goods_id,
                        'attribute_id'=>$attribute_id
                    ]);
                }
            }
            return responseZK(1,null,'设置成功');
        }else{
            return responseZK(0,null,'没有商品或属性');
        }
    }
}
